package com.zmn.plat.admin.controller.channel.workorder.picture;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.dictionary.ChannelWorkOrderPictureTypeEnum;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.workorder.picture.ChannelWorkOrderPicture;
import com.zmn.plat.model.entity.channel.workorder.picture.ChannelWorkOrderPictureQuery;
import com.zmn.plat.model.entity.channel.workorder.picture.ChannelWorkOrderPictureStandard;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 渠道-工单照片
 *
 * @author linfeng
 * @since 2020/09/17 17:29
 */
@Controller
@RequestMapping("/channel/workorder/picture/")
public class ChannelWorkOrderPictureListController extends ChannelWorkOrderPictureBaseController {

    /**
     * 列表
     *
     * @param query
     * @author linfeng
     * @since 2020/10/28 11:08
     */
    @RequestMapping("list")
    @RequiresPermissions("/channel/workorder/picture/list.action")
    public ModelAndView list(@ModelAttribute ChannelWorkOrderPictureQuery query) {

        List<ChannelWorkOrderPicture> items = translateShowCategoryName(channelWorkOrderPictureService.listPageByQuery(query));
        Channel channel = channelService.findByKey(query.getChannelId());
        if (Objects.isNull(channel)) {
            channel = new Channel();
            channel.setChannelId(query.getChannelId());
        }
        Integer mergeSettle = getMergeSettle(query.getChannelId());
        channel.setMergeSettle(mergeSettle);
        ModelAndView mav = new ModelAndView("channel/workorder/picture/list");
        mav.addObject("items", items);
        mav.addObject("obj", channel);
        mav.addObject("flag", 13);
        mav.addObject("addFlag", query.getAddFlag());
        mav.addObject("vo", query);
        return mav;
    }

    /**
     * 详情
     *
     * @param id
     * @author linfeng
     * @since 2020/10/28 11:08
     */
    @RequestMapping("addEditView")
    @RequiresPermissions("/channel/workorder/picture/addEditView.action")
    public ModelAndView detail(@RequestParam(value = "channelId", defaultValue = "0") Integer channelId,
                               @RequestParam(value = "id", defaultValue = "0") Integer id) {
        ChannelWorkOrderPicture channelWorkOrderPicture;
        if (id == 0) {
            channelWorkOrderPicture = new ChannelWorkOrderPicture();
            channelWorkOrderPicture.setRelateId(0);
            channelWorkOrderPicture.setChannelId(channelId);
        } else {
            channelWorkOrderPicture = channelWorkOrderPictureService.findByKey(id);
        }

        // 服务分类
        ServCategory querySC = new ServCategory();
        List<ServCategory> servCategories = servCategoryService.listByQuery(querySC);
        ModelAndView mav = new ModelAndView("channel/workorder/picture/detail");
        mav.addObject("obj", channelWorkOrderPicture);
        mav.addObject("servCategories", servCategories);
        mav.addObject("pictureTypes", ChannelWorkOrderPictureTypeEnum.list());

        return mav;
    }

    /**
     * 设置审核标准
     *
     * @param id
     * @author sunlife
     * @since 2021/10/27 17：00
     */
    @RequestMapping("auditStandard")
    @RequiresPermissions("/channel/workorder/picture/auditStandard.action")
    public ModelAndView auditStandard(@RequestParam(value = "channelId", defaultValue = "0") Integer channelId,
                               @RequestParam(value = "id", defaultValue = "0") Integer id) {
        ChannelWorkOrderPicture channelWorkOrderPicture;
        if (id == 0) {
            channelWorkOrderPicture = new ChannelWorkOrderPicture();
            channelWorkOrderPicture.setRelateId(0);
            channelWorkOrderPicture.setChannelId(channelId);
        } else {
            channelWorkOrderPicture = channelWorkOrderPictureService.findByKey(id);
        }

        // 服务分类
        ServCategory querySC = new ServCategory();
        List<ServCategory> servCategories = servCategoryService.listByQuery(querySC);
        ModelAndView mav = new ModelAndView("channel/workorder/picture/auditStandard");
        mav.addObject("obj", channelWorkOrderPicture);
        mav.addObject("servCategories", servCategories);
        // 照片类型
        mav.addObject("pictureTypeName", ChannelWorkOrderPictureTypeEnum.getName(channelWorkOrderPicture.getPictureType()));
        // 渠道名称
        mav.addObject("channelName", channelService.findByKey(channelWorkOrderPicture.getChannelId()).getName());

        // 渠道名称
        ServCategory query = new ServCategory();
        query.setShowStatus(GlobalConsts.YES);
        List<ServCategory> categoryList = servCategoryService.listByQuery(query);

        List<Integer> servCategIds = Arrays.asList(Optional.ofNullable(channelWorkOrderPicture.getServCategId()).orElse("").split(","))
                .stream().map(Integer::valueOf).collect(Collectors.toList());

        String servCategNames = categoryList.stream().filter(servCategory -> {
            return servCategIds.contains(servCategory.getCategId());
        }).map(ServCategory::getShowName).collect(Collectors.joining("、"));
        mav.addObject("servCategNames", servCategNames);

        List<ChannelWorkOrderPictureStandard> standardList = channelWorkOrderPictureStandardService.listStandardByRelateId(id);
        mav.addObject("standardList", standardList);


        return mav;
    }



    /**
     * 获取服务分类名称
     *
     * @param listPageByQuery 前端服务数据
     * @return 渠道工单视频数据
     */
    private List<ChannelWorkOrderPicture> translateShowCategoryName(List<ChannelWorkOrderPicture> listPageByQuery) {
        // 获取服务分类
        ServCategory query = new ServCategory();
        List<ServCategory> servCategoryList = servCategoryService.listByQuery(query);
        for (ChannelWorkOrderPicture channelWorkOrderPicture : listPageByQuery) {
            if (StringUtil.isNotBlank(channelWorkOrderPicture.getServCategId())) {
                List<String> list = Optional.ofNullable(servCategoryList.stream()
                        .filter(x -> channelWorkOrderPicture.getServCategId().contains(x.getCategId().toString()))
                        .map(ServCategory::getName)
                        .collect(Collectors.toList())).orElse(new ArrayList<>());
                channelWorkOrderPicture.setServCategName(String.join(" / ", list));
            }
        }
        return listPageByQuery;
    }
}
